import * as R from 'ramda';

export type ResourceCrudTableProps = {
  resourceName: string;
  detailColumns?: any[];
  itemColumns: any[];
  actionBtnList?: any[];
}

export function makeCommonApi(url: string) {
  return {
    "method": "post",
    "url": url,
    "data": {
      "&": "$$",
    },
  }
}

function makeListApi(resourceName: string) {
  return {
    "method": "post",
    "url": `/api/v1/${resourceName}/list`,
    "data": {
      "page": '${page}',
      "size": "${perPage}",
      "sort": "${orderBy ? " + "`${orderBy}:${orderDir}`" +" : 'createdAt:desc'}",
      "keywords": "${keywords}",
      "&": "$$",
    },
  }
}

function makeDetailApi(resourceName: string) {
  return {
    "method": "post",
    "url": `/api/v1/${resourceName}/detail`,
    "data": {
      "id": "${id}",
    },
  }
}

function makeInsertApi(resourceName: string) {
  return {
    "method": "post",
    "url": `/api/v1/${resourceName}/insert`,
    "data": {
      "&": "$$",
    },
  }
}

function makeUpdateApi(resourceName: string) {
  return makeCommonApi(`/api/v1/${resourceName}/update`);
}

function makeDeleteApi(resourceName: string) {
  return {
    "method": "post",
    "url": `/api/v1/${resourceName}/delete`,
    "data": {
      "id": "${ids ? ids : id}",
    },
  }
}


function makeDeleteButton(deleteApi:any) {
  return {
    "label": "删除",
    "type": "button",
    "actionType": "ajax",
    "level": "danger",
    "confirmText": "确认要删除？",
    "api": deleteApi
  }
}

export function makeDrawerButton({btnText, detailApi, updateApi, detailColumns}: {btnText: string, detailApi:any, updateApi:any, detailColumns:any}) {
  return {
    "label": btnText,
    "type": "button",
    "actionType": "drawer",
    "drawer": {
      "title": btnText,
      "body": {
        "type": "form",
        "initApi": detailApi,
        "api": updateApi,
        "body": detailColumns
      }
    }
  }
}

export function makeEditButton(detailApi:any, updateApi:any, detailColumns:any) {
  return makeDrawerButton({btnText: "修改", detailApi, updateApi, detailColumns});
}

export function makeCreateButton(insertApi:any, detailColumns:any) {
  const btn = makeDrawerButton({btnText: "新增", detailApi: null, updateApi: insertApi, detailColumns});
  return R.mergeLeft(btn, { "level": "primary" });
}

export function createResourceCrudTable({resourceName, detailColumns, itemColumns, actionBtnList}: ResourceCrudTableProps) {

  const listApi = makeListApi(resourceName);

  const detailApi = makeDetailApi(resourceName);

  const insertApi = makeInsertApi(resourceName);

  const updateApi = makeUpdateApi(resourceName);

  const deleteApi = makeDeleteApi(resourceName);

  return {
    "type": "crud",
    "api": listApi,
    "autoGenerateFilter": {
      "columnsNum": 3,
      "showBtnToolbar": false
    },
    "headerToolbar": [
      "bulkActions",
      {
        "type": "reload",
        "align": "right",
        "icon": "iconfont icon-refresh",
        "label": "刷新",
        "tooltip": "",
      },
      {
        "type": "export-csv",
        "label": "全量导出",
        "api": listApi
      },
      makeCreateButton(insertApi, detailColumns),
    ],
    "bulkActions": [
      {
        "label": "批量删除",
        "actionType": "ajax",
        "api": deleteApi,
        "confirmText": "确定要批量删除?"
      },
    ],
    "syncLocation": false,
    "columns": [
      {
        "name": "id",
        "label": "ID"
      },
      ...itemColumns,
      {
        "name": "createdAt",
        "label": "创建时间",
        "sortable": true,
        "type": "date",
        "fromNow": true,
        "valueFormat": "x",
        'searchable': {
          'type': 'input-datetime-range',
          "format": "x",
          'mode': 'horizontal',
        },
      },
      {
        "name": "createdBy",
        "label": "创建人"
      },
      {
        "type": "operation",
        "label": "操作",
        "buttons": [
          ...actionBtnList ?? [],
          makeEditButton(detailApi, updateApi, detailColumns),
          makeDeleteButton(deleteApi)
        ]
      }
    ]
  }
}


export type ResourceCrudListProps = {
  resourceName: string;
  detailColumns?: any[];
  cardSpec: {title:any, avatarFieldName:any, body:any, actionBtnList?:any[]};
}
export function createResourceCrudList({resourceName, detailColumns, cardSpec}: ResourceCrudListProps) {
  return {
    "type": "crud",
    "api": makeListApi(resourceName),
    "syncLocation": false,
    "mode": "cards",
    "defaultParams": {
      "perPage": 6
    },
    "switchPerPage": false,
    "placeholder": "没有店铺信息，请添加店铺",
    "columnsCount": 3,
    "headerToolbar": [
      "bulkActions",
      {
        "type": "reload",
        "align": "right",
        "icon": "iconfont icon-refresh",
        "label": "刷新",
        "tooltip": "",
      },
      makeCreateButton(makeInsertApi(resourceName), detailColumns),
    ],
    "card": {
      "header": {
        "title": cardSpec.title,
        "avatarText": "${" + cardSpec.avatarFieldName + "|substring:0:2|upperCase}",
        "avatarTextBackground": [
          "#FFB900",
          "#D83B01",
          "#B50E0E",
          "#E81123",
          "#B4009E",
          "#5C2D91",
          "#0078D7",
          "#00B4FF",
          "#008272"
        ]
      },
      "bodyClassName": "padder",
      "body": cardSpec.body,
      "actions": [
        ...cardSpec.actionBtnList ?? [],
        makeEditButton(makeDetailApi(resourceName), makeUpdateApi(resourceName), detailColumns),
        makeDeleteButton(makeDeleteApi(resourceName))
      ]
    }
  }
}

export type CustomComponentProps = {
  name: string,
  compFn: (props: {value:any, onChange:any, data:any}) => any,
}

export function createCustomComponent({name, compFn}: CustomComponentProps) {
  return {
    "name": name,
    "asFormItem": true,
    "component": compFn
  }
}